﻿using System;
using System.Linq;
using System.Web.UI.WebControls;
using XpressNews.SvcRef.AdsMgmt;
using XpressNews.SvcRef.CateMgmt;

namespace XpressNews
{
    public partial class MasterPage : System.Web.UI.MasterPage
    {
        public ServiceCategoryManagementClient cCat = null;
        private ServiceAdvertisementManagementClient cAds = null;

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                loadCat();
            }
        }

        protected void rptNavItemWithSub_ItemDataBound(object sender, System.Web.UI.WebControls.RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            {
                String parentId = ((Category)e.Item.DataItem).catId;

                Repeater rpt = ((Repeater)e.Item.FindControl("rptNavSubItem"));
                rpt.DataSource = cCat.searchCategoryByParent(parentId);
                rpt.DataBind();
            }
        }

        protected void rptNavMblItemWithSub_ItemDataBound(object sender, System.Web.UI.WebControls.RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            {
                String parentId = ((Category)e.Item.DataItem).catId;

                Repeater rpt = ((Repeater)e.Item.FindControl("rptNavMblSubItem"));
                rpt.DataSource = cCat.searchCategoryByParent(parentId);
                rpt.DataBind();
            }
        }

        protected void rptTop_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            {
                string blkName = ((ViewAdvertisementDetail)e.Item.DataItem).blkName;
                string adsImg = ((ViewAdvertisementDetail)e.Item.DataItem).advImageLink;
                string adsLink = ((ViewAdvertisementDetail)e.Item.DataItem).advLink;

                if (blkName == "top")
                {
                    ((HyperLink)e.Item.FindControl("advTop")).NavigateUrl = adsLink;
                    ((Image)e.Item.FindControl("imgAdvtop")).ImageUrl = adsImg;
                    //((HyperLink)e.Item.FindControl("advTop")).ImageUrl = adsImg;
                }
            }
        }

        protected void rptRight_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            {
                string blkName = ((ViewAdvertisementDetail)e.Item.DataItem).blkName;
                string adsImg = ((ViewAdvertisementDetail)e.Item.DataItem).advImageLink;
                string adsLink = ((ViewAdvertisementDetail)e.Item.DataItem).advLink;

                if (blkName == "right sidebar")
                {
                    ((HyperLink)e.Item.FindControl("advRight")).NavigateUrl = adsLink;
                    ((Image)e.Item.FindControl("imgAdvright")).ImageUrl = adsImg;
                }
            }
        }

        private void loadCat()
        {
            cCat = new ServiceCategoryManagementClient("catBasicHttpBinding");
            var lstCategory = cCat.searchAllCategories();            

            if (lstCategory != null)
            {
                var lst = lstCategory.Where(i => String.IsNullOrEmpty(i.catParentId)).ToList<Category>();

                //rptNavItem.DataSource = lst;
                //rptNavItem.DataBind();

                //rptNavMblItem.DataSource = lst;
                //rptNavMblItem.DataBind();

                rptNavItemWithSub.DataSource = lst;
                rptNavItemWithSub.DataBind();

                rptNavMblItemWithSub.DataSource = lst;
                rptNavMblItemWithSub.DataBind();
            }
        }

        private void loadTopAdv()
        {
            cAds = new ServiceAdvertisementManagementClient("adsBasicHttpBinding");
            var lstAdvertisement = cAds.searchAllAdvertisementDetails();

            if (lstAdvertisement != null)
            {
                var lst = lstAdvertisement.Where(i => i.blkActive == true && i.advActive == true && i.blkName == "top").ToList<ViewAdvertisementDetail>();
                 
                rptTop.DataSource = lst;
                rptTop.DataBind();
            }
        }

        private void loadRightAdv()
        {
            cAds = new ServiceAdvertisementManagementClient("adsBasicHttpBinding");
            var lstAdvertisement = cAds.searchAllAdvertisementDetails();

            if (lstAdvertisement != null)
            {
                var lst = lstAdvertisement.Where(i => i.blkActive == true && i.advActive == true && i.blkName == "right sidebar").ToList<ViewAdvertisementDetail>();

                rptRight.DataSource = lst;
                rptRight.DataBind();
            }
        }
    }
}